package com.yneusoft.common.tool;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 公共工具
 * @author  王加雷
 */
public class CommonTool {

    /**
     * mapList转为beanList
     * @param mapList map列表
     * @param beanClass Bean类型
     * @param isToCamelCase 是否下划线转驼峰
     * @param copyOptions 自定义复制类
     * @param <T>
     * @return
     */
    public static <T> List<T> mapListToBeanList(List<Map<String, Object>> mapList, Class<T> beanClass, boolean isToCamelCase, CopyOptions copyOptions) {
        //准备结果
        List<T> list = new ArrayList<>();
        //并行流转换
        mapList.parallelStream().forEachOrdered(item -> {
            list.add(BeanUtil.mapToBean(item, beanClass, isToCamelCase, copyOptions));
        });
        //返回结果
        return list;
    }

    /**
     * mapList转为beanList
     * @param mapList map列表
     * @param beanClass Bean类型
     * @param isToCamelCase 是否下划线转驼峰
     * @param <T>
     * @return
     */
    public static <T> List<T> mapListToBeanList(List<Map<String, Object>> mapList, Class<T> beanClass, boolean isToCamelCase) {
        //准备结果
        List<T> list = new ArrayList<>();
        //并行流转换
        mapList.parallelStream().forEachOrdered(item -> {
            list.add(BeanUtil.mapToBean(item, beanClass, isToCamelCase, null));
        });
        //返回结果
        return list;
    }

    /**
     * mapList转为beanList(默认下划线转驼峰)
     * @param mapList
     * @param beanClass
     * @param <T>
     * @return
     */
    public static <T> List<T> mapListToBeanList(List<Map<String, Object>> mapList, Class<T> beanClass) {
        //准备结果
        List<T> list = new ArrayList<>();
        //并行流转换
        mapList.parallelStream().forEachOrdered(item -> {
            list.add(BeanUtil.mapToBean(item, beanClass, true, null));
        });
        //返回结果
        return list;
    }

    /**
     * 普通list转换为树形list
     * @param list 要转换的数据
     * @param id             数据唯一的标识键值
     * @param pid            父id唯一标识键值
     * @param child          子节点键值
     * @return JSONArray
     */
    public static <T> JSONArray listToTree(List<T> list, String id, String pid, String child) {
        JSONArray result = new JSONArray();
        JSONObject hash = new JSONObject();
        JSONArray jsonArrayParam = JSONObject.parseArray(JSON.toJSONString(list, SerializerFeature.WriteNullNumberAsZero));

        //将数组转为Object的形式，key为数组中的id
        for (int i = 0; i < jsonArrayParam.size(); i++) {
            JSONObject json = (JSONObject) jsonArrayParam.get(i);
            hash.put(json.getString(id), json);
        }
        //遍历结果集
        for (int j = 0; j < jsonArrayParam.size(); j++) {
            //单条记录
            JSONObject aVal = (JSONObject) jsonArrayParam.get(j);
            //在hash中取出key为单条记录中pid的值
            JSONObject hashVP = (JSONObject) hash.get(aVal.get(pid).toString());
            //如果记录的pid存在，则说明它有父节点，将她添加到孩子节点的集合中
            if (hashVP != null) {
                //检查是否有child属性
                if (hashVP.get(child) != null) {
                    JSONArray ch = (JSONArray) hashVP.get(child);
                    ch.add(aVal);
                    hashVP.put(child, ch);
                } else {
                    JSONArray ch = new JSONArray();
                    ch.add(aVal);
                    hashVP.put(child, ch);
                }
            } else {
                result.add(aVal);
            }
        }
        return result;
    }
}
